/**
 * 实体
 */
Ext.namespace('Divo');
Ext.namespace('Divo.app');
Divo.app.CompanySelGridWin = Ext.extend(Ext.Window, {
      OrgGrid : null,
      queryForm : null,
      queryTextField : null,
      query : null,
      initComponent : function() {

        this.OrgGrid = this.getGrid();
        this.queryTextField = this.getField();
        this.queryForm = this.getForm();
        Ext.apply(this, {
              title : "选择科室",
              iconCls : 'icon-win',
              autoHeight : false,
              height : 450,
              width : 600,
              layout : 'border',
              plain : false,
              buttonAlign : 'right',
              closable : false,
              draggable : false,
              modal : true,
              items : [{
                    border : false,
                    layout : 'fit',
                    region : 'north',
                    height : 50,
                    items : [this.queryForm]
                  }, {
                    border : false,
                    layout : 'fit',
                    region : 'center',
                    items : [this.OrgGrid]
                  }],
              buttons : [{
                    text : '确定',
                    handler : this.selOk.createDelegate(this)
                  }, {
                    text : '关闭',
                    handler : this.onWinHide.createDelegate(this)
                  }]
            });

        Divo.app.CompanySelGridWin.superclass.initComponent.apply(this,
            arguments);
        this.addEvents('exportSelectItem');
      },

      render : function() {
        Divo.app.CompanySelGridWin.superclass.render.apply(this, arguments);
      },

      initEvents : function() {
        Divo.app.CompanySelGridWin.superclass.initEvents.call(this);
        this.on("show", this.onWinShow, this);
      },

      showDialog : function() {
        this.show();
      },

      selOk : function() {
        var record = this.OrgGrid.getSelectionModel().getSelected();
        if (record) {
          this.fireEvent("exportSelectItem", record.data.id, record.data.code,
              record.data.name);
          this.hide();
        } else {
          Ext.Msg.alert("提示", "你还没有选取科室");
        }
      },

      onQuery : function() {
        this.query = this.queryTextField.getValue();

        this.OrgGrid.getStore().load();
      },

      onWinShow : function() {
        if (this.OrgGrid) {
          this.OrgGrid.getStore().load();
        }
        this.queryTextField.setValue("");
      },

      onWinHide : function() {
        this.hide();
      },

      getField : function() {
        if (this.queryTextField) {
          return this.queryTextField;
        } else {
          var me = this;
          var textfield = new Ext.form.TextField({
                name : 'queryName',
                fieldLabel : '输入名称、拼音、五笔回车查询',
                allowBlank : true,
                width : 200,
                listeners : {
                  "specialkey" : function(o, e) {
                    if (e.getKey() === e.ENTER) {
                      me.onQuery();
                    }
                  }
                }
              });
          return textfield;
        }
      },

      getForm : function() {
        if (this.queryForm) {
          return this.queryForm;
        } else {

          var me = this;

          var queryPanel = new Ext.form.FormPanel({
                style : 'padding:0px 0px 0px 0px',
                height : "100%",
                frame : true,
                labelWidth : 100,
                labelAlign : "right",
                layout : "form",
                name : "QUERYPANEL",
                width : "100%",
                items : [this.queryTextField]
              });
          return queryPanel;
        }
      },

      getGrid : function() {

        if (this.OrgGrid) {
          return this.OrgGrid;
        } else {

          var grid = new Divo.Base.GridView({
                gridId : "query-toffices",
                //queryFlds : queryFlds,
                //searchFlds : searchFlds,
                entityClassName : 'com.our.sys.project1.entity.Toffices',
                queryUrl : 'toffices/query',
                jsonId : 'id',
                recordPk : ["id"]
              });

          grid.getStore().on('beforeload', function(store, options) {
                store.baseParams.query = this.query ? this.query : '';
              }, this, true);

          grid.on('dblclick', this.selOk.createDelegate(this), this);
          return grid;
        }
      }
    });

Ext.reg('Divo.app.CompanySelGridWin', Divo.app.CompanySelGridWin);
